<?php
GLOBAL $CFG;
GLOBAL $DB;
GLOBAL $USER;

define('AJAX_SCRIPT', true);
require_once('../../../config.php');
require_once('../lib.php');

$subject_id = $_POST["subject"];
$survey_id = $_POST["survey"];
$teacher_id = $_POST["instructor"];

$sts_id = $DB->get_record("itus_teachers_subjects", array("sts_survey_id"=>$survey_id,"sts_teacher_id"=>$teacher_id,"sts_subject_id"=>$subject_id));
$subject_infor = $DB->get_record("surveyed_subjects", array("ss_survey"=>$survey_id,"ss_subject"=>$subject_id));
$teachers = $DB->get_records_sql("select * from mdl_surveyed_teachers where st_survey = ? and st_subject = ?", array($survey_id, $subject_id));
$survey_infor = $DB->get_record("itu_survey", array("id"=>$survey_id));

//Title Survey
echo "<center><h1>".$survey_infor->itus_title."</h1></center>";


//Summary
echo "<div style=\"text-align:center;\" id=\"summary_block\">";
     echo "<div id=\"title_sum\"><h2>".$survey_infor->itus_summary_title."</h2></div>";
     echo "<div style=\"text-align:left;padding-left:120px;padding-right:120px;\" id=\"sum\">".$survey_infor->itus_summary."</div>";
echo "</div>";


//Questions
        
        echo  "<div style=\"text-align:center; cursor:hand;\" id=\"question_block\">";
            echo  "<div id=\"title_question\"><h2>".$survey_infor->itus_question_title."</h2></div>";
            echo  "<div style=\"cursor:hand;\" id=\"toggleLink\" onclick=\"$('#survey_question_block').toggle(); curState = $('#toggleLink').html(); if(curState == 'See Less'){\$('#toggleLink').html('See More')}else{\$('#toggleLink').html('See Less')};\">See Less</div>";            
        echo  "</div>";
        
        echo  "<div id=\"survey_question_block\" style=\"text-align:left;padding-left:120px;padding-right:120px;\">";
            echo  "<div id=\"question_sum\">".$survey_infor->itus_question_summary."<br/></div>";
        
            //subject name
            echo  "<u>".get_string('surveyedSubject','local_itusurveyplugin')."</u><br/>";
            echo  $subject_infor->ss_name_en."-".$subject_infor->ss_name_vn;
            
            //list of teacher
            echo  "<br/><br/><u>".get_string('teacherList','local_itusurveyplugin')."</u><br/>";
            $surveyedTeacherStr = "";
            $teacherStr = "";
            
            $teacherStr .= "<ul>";
            $surveyedTeacherStr .= "<ul>";
            foreach($teachers as $teacher){
                if(!($teacher->st_teacher == $teacher_id)){
                    $teacherStr .= "<li>".$teacher->st_fullname."</li>";
                }else{
                    $surveyedTeacherStr .= "<li><strong>".$teacher->st_fullname."</strong></li>";
                }  
            }
            $teacherStr .= "</ul>";
            $surveyedTeacherStr .= "</ul>";
            echo $teacherStr;
            //surveyed teacher
            echo $surveyedTeacherStr;
            echo "<br/>";
            //danh sách câu hỏi
            //echo  "<div id=\"questions_of_survey\" style=\"text-align:left;padding-left:120px;padding-right:120px;\">";
                $questions = $DB->get_records_sql("select ituq.id, ituq.q_content, qi.qi_order, qi.qi_itus, ituq.q_question_type
                                                    from mdl_itu_questions ituq, mdl_question_itus qi
                                                    where ituq.id = qi.qi_question and qi.qi_itus = ?
                                                    order by qi.qi_order
                                                  ", array($survey_id));
                                                  
                $questionData = array();
                $answers = array();
                foreach($questions as $question){
                    $questionData = $question;
                    echo  "<strong>".$questionData->q_content."</strong><br/>";
                    
                    //answers
                    if($questionData->q_question_type == 1){
                        echo  "<div style=\"width:100%\" name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."\">";
                        $ansValue = $DB->get_record('ans_plain', array('ap_sts'=>$sts_id->id, 'ap_question'=>$questionData->id));
                        echo  $ansValue->ap_value;
                        echo  "</div><br/><br/>";
                    }
                    if($questionData->q_question_type == 2){
                        $answers = $DB->get_records_sql("select * from mdl_ans_ddl_def where add_question = ? order by add_order asc", array($questionData->id));
                        $ansValue = $DB->get_record("ans_ddl", array("ad_sts"=>$sts_id->id,"ad_question"=>$questionData->id));
                        //$ansValue = $DB->get_record_sql("select * from ans_ddl where ad_sts = $sts_id and ad_question = ".$questionData->id);
                        foreach($answers as $answer){
                            if($ansValue->ad_value == $answer->id){
                                echo   "<div id=\"div_1_question_".$questionData->id."_".$answer->id."\"><input name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."\" value=\"".$answer->id."\" type=\"radio\" checked=\"checked\" disabled=\"disabled\"/>".$answer->add_content."</div>";
                                echo  "<br/>";       
                            }else{
                                echo   "<div id=\"div_1_question_".$questionData->id."_".$answer->id."\"><input name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."\" value=\"".$answer->id."\" type=\"radio\"  disabled=\"disabled\"/>".$answer->add_content."</div>";
                                echo  "<br/>";   
                            }
                            
                        }
                        echo  "<br/><br/>";    
                    }
                    if($questionData->q_question_type == 3){
                        $answers = $DB->get_records_sql("select * from mdl_ans_cbx_def where acd_question = ? order by acd_order asc", array($questionData->id));
                        $ansValues = null;
                        foreach($answers as $answer){
                            $ansValues = $DB->get_records("ans_cbx", array("ac_sts"=>$sts_id->id,"ac_question"=>$questionData->id,"ac_value"=>$answer->id));
                            if(count($ansValues) > 0){
                                echo   "<div id=\"div_3_question_".$questionData->id."_".$answer->id."\"><input name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."_".$answer->id."\" value=\"".$answer->id."\" type=\"checkbox\" checked=\"checked\" disabled=\"disabled\"/>".$answer->acd_content."</div>";
                                echo  "<br/>";
                            }else{
                                echo   "<div id=\"div_3_question_".$questionData->id."_".$answer->id."\"><input name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."_".$answer->id."\" value=\"".$answer->id."\" type=\"checkbox\" disabled=\"disabled\"/>".$answer->acd_content."</div>";
                                echo  "<br/>";
                            }                            
                        }       
                        echo  "<br/><br/>";
                    }
                    if($questionData->q_question_type == 4){
                        $answers = $DB->get_records_sql("select * from mdl_ans_scale_def where asd_question = ?", array($questionData->id));
                        $ansValue = $DB->get_record("ans_scale", array("as_sts"=>$sts_id->id,"as_question"=>$questionData->id));
                        foreach($answers as $answer){
                            $up = $answer->asd_upbound;
                            $down = $answer->asd_lowbound;
                            for($i = $down; $i <= $up; $i++){
                                if($ansValue->as_value == $i){
                                    echo   "<div style=\"float:left\" id=\"div_4_question_".$questionData->id."_".$i."\"><input type=\"radio\" value=\"".$i."\" name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."\" checked=\"checked\" disabled=\"disabled\"/>".$i."</div>";    
                                }else{
                                    echo   "<div style=\"float:left\" id=\"div_4_question_".$questionData->id."_".$i."\"><input type=\"radio\" value=\"".$i."\" name=\"question_".$questionData->id."\" id=\"question_".$questionData->id."\" disabled=\"disabled\"/>".$i."</div>";
                                }
                            }       
                        }
                        echo  "<br/><br/>";       
                    }  
                }
        echo  "</div>";


//LOs
$mylib = new survey_management();

echo "<div style=\"text-align:center;\" id=\"lo_block\">";
	echo "<div id=\"title_lo_sum\"><h2>".$survey_infor->itus_lo_title."</h2></div>";
	echo "<div style=\"text-align:left;padding-left:120px;padding-right:120px;\" id=\"lo_sum\">";
	echo $survey_infor->itus_lo_summary;
	echo "</div>";
	echo "<div id=\"los_of_survey\" style=\"text-align:left;padding-left:120px;padding-right:120px;\">";
			echo "<div style=\"width:60%; float:left; font-weight:bold;\">Tiêu chuẩn đầu ra</div><div style=\"width:10%; float:left; font-weight:bold;\">I/T/U</div><div style=\"width:30%; float:left; font-weight:bold;\">Hoạt động  giảng dạy và đánh giá </div>";
			//recursive process to display LOs
				$outputStr = "";
				$mylib->displayChildLoDownToLevInReview($survey_id, '1', $survey_infor->itus_depth_lo, "0", $mylib, $outputStr, $sts_id->id);
				echo $outputStr;
	echo "</div>";
echo "</div>";
?>